﻿using NPOI.HSSF.UserModel;
using OMG.Fill.Mulberry.Model.Ent;
using OMG.Fill.Mulberry.Service;
using OMG.Fill.Mulberry.UI.ActionSafety;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Mulberry.Model.UI.Controllers
{
    public class UserController : ApiController
    {
        UserServices userServices = new UserServices();
        AreaServices area = new AreaServices();
        
        //[ApiActionAuthorize("获取用户表T_User分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<UserModel>> PostPageList(UserQuery query)
        {
            return userServices.GetPageList(query);
        }
        //[ApiActionAuthorize("新增用户表T_User指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(UserEntity model) {
            var user = AppUser.GetUser();
            model.AddUser = AppUser.GetUser().Account;
            model.AddTime = DateTime.Now;
            return userServices.AddInfo(model);
        }
        public Pager<List<UserStatsModel>> PostStatsList(MulNowModel model)
        {
            return userServices.GetUserStatsList(model);
        }
        //[ApiActionAuthorize("修改用户表T_User指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(UserEntity model)
        {
            model.UpdateUser = AppUser.GetUser().Account;
            model.UpdateTime = DateTime.Now;
            return userServices.UpdateInfo(model);
        }
        //[ApiActionAuthorize("删除用户表T_User指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return userServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取用户表T_User指定对象操作")]
        //[ApiActionException]
        public ResResult<UserModel> Get(string id)
        {
            return userServices.GetInfo(id);
        }
        public ResResult<UserModel> Get(string AreaCode,string Account)
        {
            return userServices.GetUserInfo(AreaCode,Account);
        }
        /// <summary>
        /// 重置用户密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResResult<int> GetResetPwd(string id)
        {
            return userServices.UpdatePassword(id, 0, "", "");
        }

        /// <summary>
        /// 检查用户名是否存在
        /// </summary>
        /// <param name="Account"></param>
        /// <returns></returns>
        public ResResult<UserEntity> GetCheckAccount(string Account)
        {
            return userServices.GetModel(Account);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        public ResResult<int> PostPassword(UserPasswordModel model)
        {
            return userServices.UpdatePassword(model.UserId, 1, model.Password, model.OriginalPwd);
        }


        public void GetStatsExcelData(string FillInYear, string AreaCode, string Status)
        {
            MulNowModel model = new MulNowModel();
            model.FillInYear = FillInYear;
            model.AreaCode = AreaCode;
            model.Status = Status;
            //string ProvinceCode = "530000000000";
            AreaServices area = new AreaServices();
            List<UserStatsModel> list = new List<UserStatsModel>();
            if (!string.IsNullOrEmpty(model.AreaCode))
            {
                var listMulArea = userServices.GetUserStatsList(model);
                list = listMulArea.rows;
            }
            //string AreaName = area.GetByCodeModel(model.AreaCode).model.Name;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = "";
            if (AreaCode != DefaultKey.ProvinceCode) {
                string Name = area.GetByCodeModel(AreaCode).model.Name;
                strFileName = FillInYear + Name + "登录人员信息采集表.xls";
            }else
            {
                strFileName = FillInYear + "全省登录人员信息采集表.xls";
            }
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(userServices.MethodStats(list).GetBuffer());
            curContext.Response.End();
        }
    }
}